/****************************************************
    ScriptName:        Boss5.cs
    Author:            摸鱼程序男
    CreatTime:    2020/12/27 2:38:53
    Description:     Nothing
*****************************************************/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
using DG.Tweening;
using System.Linq;
using BTAI;
using ZYKTool;
using ZYKTool.Timer;
using LongZhiJie;


namespace HQL
{
    public class Boss5 : BossBase
    {
        public GameObject _子弹;
        private int _damage;
        private float _speed;
        private bool _is隐身;
        private int _受伤 = 0;
        public override void Init()
        {

            base.Init();
            _aiRoot.OpenBranch(

                BT.If(CanAttack).OpenBranch(
                    BT.Call(disapper),
                    BT.Call(Attack)
                )


            /*
           BT.While(die1).OpenBranch(
                 BT.Wait(1),
                 BT.Call(换阶段)
                  )
            */
            );
            _AttackTimer = 2;
            _SR = transform.GetOrAddComponent<SpriteRenderer>();
            _MyColor = _SR.color;
            _damage = 2;
            _speed = 35;
            _Blood = TapTapStaticValue.BossAttributes[BossName.Boss5].Blood;
            BossName = BossName.Boss5;
        }

        public override void BossHunt(float damage)
        {
            _受伤++;
            if (_受伤 >= 5)
            {
                Shoot2(_damage, _speed);
                _受伤 = 0;
            }
            TapTapStaticValue.TapTapModelManger.Damage(BossName.Boss5, damage);
            _Blood = TapTapStaticValue.BossAttributes[BossName.Boss5].Blood;
            if (TapTapStaticValue.BossAttributes[BossName.Boss5].Blood <= 0)
            {
                PlaySound(2);
                Die(BossName.Boss5);
                TapTapStaticValue.TapTap升级系统.TapTapStartUpdate();
            }
            else if (TapTapStaticValue.BossAttributes[BossName.Boss5].Blood <=1000 && 当前阶段 < 2)
            {
                换阶段();
            }
        }

        protected override void Attack()
        {

            if (_已经死亡) return;
            //Debug.LogError("attack");
            switch (当前阶段)
            {
                case 1:
                    Attack1();
                    break;
                case 2:
                    Attack1();
                    //TODO:贺启亮 换阶段之后如果需要改变参数写这里
                    break;
            }
        }
        public override void 换阶段()
        {
            base.换阶段();
            //Debug.LogError("二级模式切换阶段！");
            _AttackTimer = 1f;
            _damage = 3;
            _speed = 45;
            _is隐身 = true;
            //todo:贺启亮根据血量 让他换阶段就行
        }
        protected void Attack1()
        {

            if (UnityEngine.Random.Range(0, 15) < 8)
            {
                //发射子弹
                ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                    (() =>
                    {

                        if (_已经死亡) return;
                        Shoot1(_damage, _speed);

                    },
                    0.5f, 5, PETimeUnit.Second);//三到五秒才射击
                //发射子弹
                ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                    (() =>
                    {
                        if (_已经死亡) return;
                        _AttackOver = true;


                    },
                    3f, 5, PETimeUnit.Second);//三到五秒才射击




            }
            else if (UnityEngine.Random.Range(0, 15) >= 6 && UnityEngine.Random.Range(0, 15) < 12)
            {

                dash();
            }
            else if(UnityEngine.Random.Range(0, 15) >= 12 && UnityEngine.Random.Range(0, 15) < 15)
            {
                Shoot(_damage, _speed);
            }

        }

        protected void Shoot(int damage, float speed)
        {

            if (_已经死亡) return;
            //Debug.LogError("shot");

            PlaySound();
            disapper();
            int j = 0;
            //Vector3 dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
            for (int i = 0; i < 12; i++, j++)//散弹枪
            {
                // Debug.LogError(transform.rotation);

                Vector3 dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
                GameObject bullet = Instantiate(_子弹, transform.position, Quaternion.Euler(dir));
                BossBullet bossbullet = bullet.GetOrAddComponent<BossBullet>();
                bossbullet.Damage = damage;
                bossbullet.speed = speed;
                bossbullet.dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
                bullet.transform.Rotate(new Vector3(0, 0, j * 30));
                _AttackOver = true;

            }
            return_color();
        }
        protected void Shoot1(int damage, float speed)
        {
            disapper();
            PlaySound();
            int j = 0;
            //Vector3 dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
            for (int i = 0; i < 3; i++, j++)//散弹枪
            {
                // Debug.LogError(transform.rotation);

                // Vector3 dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
                GameObject bullet = Instantiate(_子弹, transform.position, Quaternion.identity);
                BossBullet bossbullet = bullet.GetOrAddComponent<BossBullet>();
                bossbullet.Damage = damage;
                bossbullet.speed = speed;
                bossbullet.dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
                bullet.transform.Rotate(new Vector3(0, 0, j * 8));
                _AttackOver = true;
                



            }
           
        }
        protected void dash()
        {

            disapper();
            PlaySound();
            transform.DOKill();
            // Debug.LogError("dash");
            transform.DOMove(TapTapStaticValue.Player.transform.position, 2);
            //var dashStop = UnityEngine.Random.Range(0, 1);
            ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                 (() =>
                 {
                     if (_已经死亡) return;
                     transform.DOKill();
                    
                     _AttackOver = true;
                 }, 2, 1);//停止撞击
            
        }
        protected void disapper()
        {

            if (_is隐身)
            {
                _SR.color = Color.clear;
            }
            
        }
        protected void Shoot2(int damage, float speed)
        {
            ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                       (() =>
                       {

                           if (_已经死亡) return;
                           disapper();
                           int j = 0;
                           //Vector3 dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
                           for (int i = 0; i < 5; i++, j++)//散弹枪
                           {
                               // Debug.LogError(transform.rotation);

                               // Vector3 dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
                               GameObject bullet = Instantiate(_子弹, transform.position, Quaternion.identity);
                               BossBullet bossbullet = bullet.GetOrAddComponent<BossBullet>();
                               bossbullet.Damage = damage;
                               bossbullet.speed = speed + 10f;
                               if (UnityEngine.Random.Range(0, 15) < 5)
                               {
                                   bossbullet.transform.Rotate(new Vector3(0, 0, j * 30));
                               }
                               else if (UnityEngine.Random.Range(0, 15) >= 5 && UnityEngine.Random.Range(0, 15) < 10)
                               {

                                   bossbullet.transform.Rotate(new Vector3(0, 0, j * 20));
                               }
                               else
                               {

                                   bossbullet.transform.Rotate(new Vector3(0, 0, j * 20));
                               }
                               bossbullet.dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
                               
                              



                           }

                       },
                       0.2f, 3, PETimeUnit.Second);//三到五秒才射击
                                                   //发射子弹
            ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                (() =>
                {
                    if (_已经死亡) return;
                    return_color();
                    


                },
                1f, 3, PETimeUnit.Second);//三到五秒才射击
            
        }
        protected void return_color()
        {

            ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                    (() =>
                    {
                        if (_已经死亡) return;
                        _SR.color = _MyColor;
                        
                    },
                    5f, 1);//三到五秒才射击
        }
    }
    
}